﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace KrisThom.Hash
{
    static class CalculateHash
    {
        public static string GetMD5HashFromFile(string fileName)
        {
            StringBuilder sb = new StringBuilder();

            try
            {
                byte[] retVal;
                using (FileStream file = new FileStream(fileName, FileMode.Open))
                {
                    using (MD5 md5 = new MD5CryptoServiceProvider())
                    {
                        retVal = md5.ComputeHash(file);
                    }
                }

                foreach (byte hex in retVal)
                {
                    sb.Append(hex.ToString("x2"));
                }

            }
            catch
            {
                sb.Append("ERROR");
            }
        

            return sb.ToString();
        }
    }
}
